package com.platform.push.util;

/**
 * @author ：mongo
 * @date ：2021-04-09 18:25:16
 * @description：
 * @modified By：
 * @version: 1.0.0
 */

import com.google.gson.Gson;
import okhttp3.OkHttpClient;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class ApiUtil {
    public static <I> I createApi(String url, Class<I> clz) {
        return createApi(url, clz, false);
    }

    public static <I> I createApi(String url, Class<I> clz, boolean debug) {
        OkHttpClient.Builder builder = new OkHttpClient.Builder();
        if (debug) {
            // Log信息拦截器
            HttpLoggingInterceptor loggingInterceptor = new HttpLoggingInterceptor();
            loggingInterceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
            // 设置 Debug Log 模式
            builder.addInterceptor(loggingInterceptor);
        }
        OkHttpClient client = builder.build();

        Gson gson = new Gson();
        return new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .client(client)
                .build()
                .create(clz);
    }
}
